package com.md.drcs.dicom.dao;

import com.md.common.utils.dao.MyMapper;
import com.md.drcs.dicom.dto.PatientStudyDTO;
import com.md.drcs.dicom.dto.StudyBasicInfoDTO;
import com.md.drcs.dicom.dto.UserPatientDTO;
import com.md.drcs.dicom.dto.UserPatientStudyDTO;
import com.md.drcs.dicom.entity.StudyEntity;
import com.md.drcs.dicom.req.PatientStudyQueryReq;
import com.md.drcs.dicom.sql.StudySQLBuiler;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

public interface StudyDao extends MyMapper<StudyEntity> {

    @SelectProvider(type = StudySQLBuiler.class, method = "findPatientStudy")
    List<PatientStudyDTO> findPatientStudy(PatientStudyQueryReq req);

    @Select("select" +
            " s.nStuId, s.nPatId, s.sStuAge, s.sMod, s.sRequDept," +
            " s.stuDate, p.sSex, p.sName, h.sHospName" +
            " from t_study s" +
            " left join t_patient p on (s.nPatId = p.nPatID) " +
            " left join t_hospitalinfo h on (p.sHospId = h.sHospId)" +
            " where s.nPatID = #{patientId}" +
            " order by nStuId desc limit 0, 1")
    UserPatientDTO getNewestStudy(Integer patientId);

    @Select("select nStuId, sHospId, nPatID from t_study where sStuIdInPacs = #{sStuIdInPacs} limit 0, 1")
    StudyEntity findByStudyIdInPacs(String sStuIdInPacs);

    @Select("select" +
            " p.nPatId, p.sHospPatId, p.sHospId, p.sName," +
            " p.sEName, p.sSex, p.sPatIdInPacs," +
            " s.nStuId, s.sStuIdInPacs, s.sStuAge, s.stuDate, s.sStuPart," +
            " s.sRequDept, s.sHosNo, s.sBedNo, s.sMod, s.diskCode, s.bCloseCloud, h.sHospName," +
            " (select case when r.AudDate is null then 0 else 1 end from t_report r where r.nStuId = s.nStuId limit 0, 1) `status`,s.sStuStatus" +
            " from t_study s" +
            " left join t_patient p on (s.nPatId = p.nPatID)" +
            " left join t_hospitalinfo h on (p.sHospId = h.sHospId)" +
            " where p.nPatId = #{patientId}")
    List<UserPatientStudyDTO> findByPatientId(Long patientId);

    @Select("select s.nStuId, s.nPatID" +
            " from t_report r" +
            " left join t_study s on (r.nStuId = s.nStuId)" +
            " where (s.notify_status = 0 or s.notify_status is null)" +
            " and s.sStuStatus in ('已审核', '已打印')" +
            " and r.AudDate > DATE_SUB(NOW(), INTERVAL 30 MINUTE)")
    List<StudyEntity> findNotNotifyAuditedStudy();

    @Select("select s.nStuId, s.nPatID" +
            " from t_report r" +
            " left join t_study s on (r.nStuId = s.nStuId)" +
            " where (s.notify_status = 0 or s.notify_status is null)" +
            " and r.AudDate > DATE_SUB(NOW(), INTERVAL 30 MINUTE)" +
            " and s.nPatID = #{patientId}")
    List<StudyEntity> findNotNotifyAuditedByPatientId(Integer patientId);

    @Update("<script>" +
            "update t_study set notify_status = 1 where nStuId in " +
            " <foreach collection='studyIds' item='sid' open='(' separator=',' close=')'>" +
            " #{sid}" +
            " </foreach>" +
            "</script>")
    void updateNotifyStatus(@Param("studyIds") List<Long> studyIds);

    @Select("<script>" +
            "select p.nPatId, p.sName, p.sPatIdInPacs, p.sPhoneTel mobile, p.sHospId hospId, " +
            " s.sHospStuid, s.nStuId, s.sStuAge, s.stuDate, s.sStuPart, s.sMod, s.nRecordStatus " +
            " from t_study s" +
            " left join t_patient p on (s.nPatId = p.nPatID)" +
            " where s.nStuId in " +
            " <foreach collection='studyIds' item='sid' open='(' separator=',' close=')'>" +
            " #{sid}" +
            " </foreach>" +
            "</script>")
    List<StudyBasicInfoDTO> findBasicInfoByIds(@Param("studyIds") List<Long> studyIds);

    @Select("select * from t_study where sHospStuid = #{hospStudyId} and sHospid = #{hospId} order by nStuId desc limit 0, 1")
    StudyEntity getBasicByHospStudyId(@Param("hospStudyId") String hospStudyId, @Param("hospId") String hospId);

    @Select("select * from t_study where sStuIdInPacs = #{stuIdInPacs} and sHospid = #{hospId} order by nStuId desc limit 0, 1")
    StudyEntity getBasicByStuIdInPacs(@Param("stuIdInPacs") String stuIdInPacs, @Param("hospId") String hospId);

    @Select("select * from t_study where sAccessNumber = #{accessNumber} and sHospid = #{hospId} limit 0, 1")
    StudyEntity getByAccessNumber(@Param("accessNumber") String accessNumber, @Param("hospId") String hospId);

    @Select("select * from t_study where sHosNo = #{hsopNo} and sHospid = #{hospId} limit 0, 1")
    StudyEntity getByHospNo(@Param("hsopNo") String hsopNo, @Param("hospId") String hospId);

    @Select("<script>" +
            "select" +
            " p.nPatId, p.sHospPatId, p.sHospId, p.sName," +
            " p.sEName, p.sSex, p.sPatIdInPacs," +
            " s.nStuId, s.sStuIdInPacs, s.sStuAge, s.stuDate, s.sStuPart," +
            " s.sRequDept, s.sHosNo, s.sBedNo, s.sMod, s.diskCode, s.bCloseCloud, " +
            " (select case when r.AudDate is null then 0 else 1 end from t_report r where r.nStuId = s.nStuId limit 0, 1) `status`,s.sStuStatus" +
            " from t_study s" +
            " left join t_patient p on (s.nPatId = p.nPatID)" +
            " where p.sName = #{name}" +
            "<if test='excludeStudyIds != null and excludeStudyIds.size() > 0'>" +
            " and s.nStuId not in " +
            " <foreach collection='excludeStudyIds' item='sid' open='(' separator=',' close=')'>" +
            " #{sid}" +
            " </foreach>" +
            "</if>" +
            "<if test='hospNos != null and hospNos.size() > 0'>" +
            " and s.sHosNo in" +
            " <foreach collection='hospNos' item='hospNo' open='(' separator=',' close=')'>" +
            " #{hospNo}" +
            " </foreach>" +
            "</if>" +
            "</script>")
    List<UserPatientStudyDTO> findByHosNos(@Param("hospNos") List<String> hospNos, @Param("name") String name, @Param("excludeStudyIds") List<Long> excludeStudyIds);

    @Select("select filmPrintDate from t_study where sStuIdInPacs = #{stuidInPacs} ")
    LocalDateTime getPrintTime(String stuidInPacs);
}
